package com.youzi.mdtool.controller;

import com.youzi.mdtool.conf.MDConfig;
import com.youzi.mdtool.entity.Catalog;
import com.youzi.mdtool.util.R;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 首页
 *
 * @author ：Andrew.Song
 * @Date : 2018-8-27 18:02
 */
@Controller
public class IndexController {

    private static final Logger log = LoggerFactory.getLogger(IndexController.class);
    @Autowired
    private MDConfig mdConfig;

    @RequestMapping(value = "/")
    public String index(Model model) {
        model.addAttribute("welcome",mdConfig.getWelecome());
        return "index";
    }

    @GetMapping(value = "queryWelcome")
    @ResponseBody
    public R queryCatalog() {
        R r = new R(R.SUCCESS);
        Resource classPathResource = new ClassPathResource("welcome.md");
        try {
            InputStream inputStream = classPathResource.getInputStream();
            String string = IOUtils.toString(inputStream,"UTF-8");
            r.setObject(string);
            return r;
        } catch (IOException e) {
            log.error("读取markdown内容,系统异常:", e);
            r.setCode(R.ERROR);
            r.setMsg("系统异常");
            return r;
        }
    }
}
